package com.asa.jijiji;

import java.util.ArrayList;
import java.util.List;

/**
 * 集合
 * @author Administrator
 *
 */
public class JiHe <T extends Comparable<? super T>>{
	
	
	/**
	 * 求两个集合的和
	 * @param a
	 * @param b
	 * @return
	 */
	public List<T> he(List<T> a,List<T> b){
		List<T> result = new ArrayList<T>();
		result.addAll(a);
		for (int i = 0; i < b.size(); i++) {
			T t = b.get(i);
			if (!(contains(result,t)>=0)) {
				result.add(t);
			}
		}
		return result;
		
	}
	
	/**
	 * 求两个集合的减法
	 * @param a
	 * @param b
	 * @return
	 */
	public List<T> jian(List<T> a,List<T> b){
		List<T> result = new ArrayList<T>();
		result.addAll(a);
		for (int i = 0; i < b.size(); i++) {
			T t = b.get(i);
			int contains_index = contains(result,t);
			if (!(contains_index>=0)) {
				result.remove(contains_index);
			}
		}
		return result;
		
	}
	
	
	
	/**
	 * 求两个集合的交
	 * @param a
	 * @param b
	 * @return
	 */
	public List<T> jiao(List<T> a,List<T> b){
		List<T> result = new ArrayList<T>();
		for (int i = 0; i < b.size(); i++) {
			T t = b.get(i);
			if (contains(a,t)>=0) {
				result.add(t);
			}
		}
		return result;
		
	}
	
	/**
	 * 求集合的补集
	 * @param father
	 * @param son
	 * @return
	 */
	public List<T> bu(List<T> father,List<T> son){
		if (son.size()>father.size()) {
			throw new RuntimeException("参数错误，son不是father的子集");

		}
		for (int i = 0; i < son.size(); i++) {
			T t = son.get(i);
			if (contains(father, t)<0) {
				throw new RuntimeException("参数错误，son不是father的子集");
			}
		}
		
		List<T> result = new ArrayList<T>();
		result.addAll(father);
		
		for (int i = 0; i < son.size(); i++) {
			T t = son.get(i);
			int contains_index = contains(result, t);
			if (contains_index>=0) {
				result.remove(contains_index);
			}
		}
		return result;

	}
	
	
	
	
	
	/**
	 * 集合中是否包含某个元素
	 * @param a
	 * @param b
	 * @return
	 */
	public int contains(List<T> a ,T b) {
		for (int i = 0; i < a.size(); i++) {
			T t = a.get(i);
			int compareTo = t.compareTo(b);
			if (compareTo==0) {
				return i;
			}
		}
		return -1;
	}
	
	
	
	
	
	
	
	
	

}

